<template>
  <div class="custom-body" :style="borderStyle">
    <div class="custom-content">
      <div class="custom-header">
        <div class="header-input">
          <div class="header-input-span">
            <input v-model="query.blurry" placeholder="搜索您需要的服务 / Search Service" @keyup="queryObject">&nbsp;&nbsp;&nbsp;
            <div class="header-input-icon" @click="queryObject">
              <i class="el-icon-search" />
            </div>
          </div>
        </div>
        <div class="custom-header-li">
          <query-item v-for="(item,index) in queryList" :key="'query-'+index" :layout="layout" :params="item" @call-back="queryClick" />
        </div>
      </div>
      <el-empty v-if="dataList === null || dataList.length === 0" :image-size="100" />
      <div v-for="(item,index) in dataList" v-else :key="'server-li-'+index" class="custom-content-ul">
        <div class="content-header">{{ item.bmmc }} <i class="title-i">({{ item.dataList === null ? 0: item.dataList.length }})</i></div>
        <div class="content-body">
          <div v-for="(obj,val) in item.dataList" :key="'server-'+index + '-'+val" class="body-li" @click="openEntity(obj)">
            <div class="li-image"> <el-image lazy class="custom-image" :src="baseURL+obj.pcIcon" /></div>
            <div class="li-title">{{ obj.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import QueryItem from './slot/item'
import { getToken } from '@/utils/auth'
import { queryAllServiceNoAuthByOrganization, queryAllServiceAuthByOrganization } from '@/api/portal/manager/setting/index'
export default {
  name: 'AppCustomServiceV1',
  components: { QueryItem },
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      dataList: [],
      column: [],
      queryList: [],
      baseURL: process.env.VUE_APP_BASE_API.substring(0, process.env.VUE_APP_BASE_API.length - 1),
      query: {
        blurry: ''
      },
      customStyle: {},
      titleStyle: {},
      bodyStyle: {}
    }
  },
  computed: {
    borderStyle() {
      if (this.temp.sfdzqp === true) {
        return { position: 'absolute', left: '0px', right: '0px', top: '0px', bottom: '0px' }
      }
      return { height: this.temp.defaultHeight }
    },
    headerStyle() {
      if (this.temp.sfzdy === true) {
        return { backgroundColor: this.temp.backgroundColor, color: this.temp.fontColor }
      }
      return {}
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData
        this.titleStyle = { backgroundColor: newData.mrlb, opacity: newData.tmd }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        this.query = {
          blurry: ''
        }
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx)
          this.column = pzx
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.column = pzx
        }
        if (undefined !== this.column.query && this.column.query !== null) {
          this.queryList = this.column.query
        } else {
          this.queryList = []
        }
        this.initData()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    queryObject() {
      this.initData()
    },
    openEntity(data) { // 打开详情页
      if (this.column.isOpenEntity) { // 允许打开详情页
        const params = []
        let url = ''
        if (undefined !== this.column.entityParams && this.column.entityParams !== null && this.column.entityParams.length > 0) {
          this.column.entityParams.forEach(item => {
            const obj = { code: item.code, value: data[item.value] }
            params.push(obj)
          })
        }
        if (this.column.entityOpenType === 1) { // 地址参数的方式打开
          url = data[this.column.entityUrl]
        }
        if (this.column.entityOpenType === 2) { // 地址传参查询
          if (undefined !== this.column.params && this.column.params !== null && this.column.params.length > 0) {
            url = this.column.entityUrl
            this.column.params.forEach(item => {
              url = url.replace(new RegExp('\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'), data[item.column])
            })
          }
        }
        const object = { title: data.name, params: params, setting: this.temp, entityTemp: this.column.entityTemplateId, temp: this.column.entityTemp, openType: this.column.entityOpenType, url: url }
        this.$emit('fetch-entity', object)
      }
    },
    initData() {
      if (getToken()) {
        queryAllServiceAuthByOrganization(this.query).then(res => {
          const { data } = res
          this.dataList = data.data
        }).catch(() => {
          this.dataList = []
        })
      } else {
        queryAllServiceNoAuthByOrganization(this.query).then(res => {
          const { data } = res
          this.dataList = data.data
        }).catch(() => {
          this.dataList = []
        })
      }
    },
    queryClick(obj) {
      this.query[obj.code] = obj.value
      this.initData()
    }
  }
}
</script>

<style scoped>
.custom-content {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #ffffff;
}
.header-input-span > input {
  width: 100%;
  border: none;
  outline: none;
}
.custom-header {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  background-color: #F2F6FC;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
}
.custom-content-ul {
  margin-top: 10px;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  background-color: #F2F6FC;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
}
.header-input {
  width: 100%;
  line-height: 40px;
  height: 40px;
  padding: 5px 15px;
  box-sizing: border-box;
  font-size: 13px;
}
.header-input-span {
  width: 100%;
  height: 100%;
  border-radius: 18px;
  background-color: #ffffff;
  padding: 0px 20px;
  box-sizing: border-box;
  position: relative;
  line-height: 30px;
  color: #606266;
}
.header-input-icon {
  position: absolute;
  right: 5px;
  top: 0px;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
}
.custom-header-li {
  width: 100%;
  padding: 0px 15px;
  display: flex;
  flex-flow: row wrap;
}
.custom-header-li:not(:last-child) {
  border-bottom: #E4E7ED dashed 1px;
}

.content-header {
  width: 100%;
  height: 40px;
  line-height: 40px;
  border-left: 4px #0a84ff solid;
  text-indent: 10px;
  border-bottom: 1px solid #E4E7ED;
}
.content-body {
  width: 100%;
  display: flex;
  flex-flow: row wrap;
}
.body-li {
  width: 33.333%;
  height: 90px;
  flex: 0 1 auto;
  padding: 8px;
  box-sizing: border-box;
}
.li-image {
  width: 100%;
  height: 48px;
  padding: 4px 0px;
  text-align: center;
  box-sizing: border-box;
}
.custom-image {
  height: 30px;
  width: 30px;
  margin: 0 auto;
}
.li-title {
  line-height: 13px;
  text-align: center;
  font-size: 13px;
}
</style>
